// stores/user.ts
import { getUserInfo } from '@/api/rbacApi';
import { addPage, loadComponent } from '@/router';
import { defineStore } from 'pinia'
import { defineAsyncComponent } from 'vue';

export const useUserStore = defineStore('userInfo', {
    state: () => ({
        user: {},
        //菜单权限
        permissionMenuTree: [] as any[],
        //按钮权限
        permissionBtn: {},
        //按钮刷新状态
        isRoutesLoaded: false
    }),
    getters: {
        getUser: (state): any => {
            return state.user;
        },
        getPermissionMenuTree: (state): any => {
            return state.permissionMenuTree
        },
        getPermissionBtn: (state): any => {
            return state.permissionBtn
        },
        getIsRoutesLoaded: (state) => {
            return state.isRoutesLoaded
        }
    },
    actions: {
        setUser(user: any) {
            if (user)
                this.$state.user = user
        },
        setPermission(permissionResource: any) {            
            if(permissionResource.menuTree){
                afterInit(permissionResource.menuTree);
                this.$state.permissionMenuTree = permissionResource.menuTree;
            }
            if(permissionResource.permission){
                this.$state.permissionBtn = permissionResource.permission
            }
        },
        loadRouter(){
            if(this.$state.isRoutesLoaded){
                return false
            }else{
                this.$state.isRoutesLoaded = true
                return true
            }
        }
    }
})

//加载路由
export function loaderRouter(){
    let userStore = useUserStore()
    let res = [] as any[]

    if(userStore.getIsRoutesLoaded){
        return []
    }else{
        if(userStore.loadRouter()){
            for (let item of userStore.getPermissionMenuTree) {
                toRouter(res, item,[])
            }
            return res
        }else{
            return []
        }
    }
}


//路由转换
function toRouter(rootRouter : any[],router : any,parentResourceCode :string[]){
    let sonCodeArr = [...parentResourceCode];
    sonCodeArr.push(router.resourceCode)

    let addFlag = router.resourceType == 'M'    
    if(addFlag){       
        let tmpEle = {
            name:router.extInfo.name,
            path:router.extInfo.path,
            component: ()=>import(`/src/views${router.extInfo.component}.vue`),
            meta:{
                menuId:router.resourceCode,
                parentResourceCode:sonCodeArr
            },
            children:[] as any[]
        }
        
        rootRouter.push(tmpEle)
        rootRouter = tmpEle.children
    }

    for(let sonItem of router.sonList || []){
        toRouter(rootRouter,sonItem,[...sonCodeArr])
    }

    return null;
}

//remove /
function afterInit(menuTree: any[]) {
    for (const item of menuTree) {
        if(item.resourceType == 'M'){            
            let extInfo = item.extInfo
            if(extInfo){
                let str = extInfo.path
                if(str){
                    extInfo.path = str.startsWith("/") ? str.slice(1) : str;
            
                    if(item.children){
                        afterInit(item.children)
                    }
                }
            }
        }else{
            menuTree = item.sonList
            if(menuTree)
                afterInit(menuTree)
        }
    }
}
